package com.tiny.codegen.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * 读取属性文件
 * 
 * @author Tiny</br>
 *         mail: 114740122@qq.com
 * @date 2018年7月10日  
 * @version 1.0
 */
public class PropertyUtil {

	/**
	 * 属性文件读取
	 * 
	 * @param path
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public static Map<String, String> readProperties(String path, String fileName) throws Exception {
		InputStream in = null;
		Map<String, String> proMap = new HashMap<String, String>();
		try {
			Properties prop = new Properties();
			in = new BufferedInputStream(new FileInputStream(new File(path, fileName)));
			prop.load(in);
			Iterator<String> it = prop.stringPropertyNames().iterator();
			while (it.hasNext()) {
				String key = it.next();
				proMap.put(key, prop.getProperty(key));
			}
			return proMap;
		} catch (Exception e) {
			throw new Exception("属性文件读取异常", e);
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 加载properties
	 * @param path
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public static Properties loadProperties(final String path, final String fileName) throws Exception {
		InputStream in = null;
		Properties prop = new Properties();
		try {
			prop.load(new FileInputStream(new File(path, fileName)));
		} catch (Exception e) {
			throw new Exception("属性文件读取异常", e);
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return prop;
	}

}
